#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int w[N], q[N];
LL tree[N];
int n, m;
int lowbit(int x)
{
	return x & -x;
}
void add(int x, LL v)
{
	for (int i = x; i <= m; i += lowbit(i)) {
		tree[i] = max(tree[i], v);
	}
}
LL get_sum(int x)
{
	LL res = 0;
	for (int i = x; i; i -= lowbit(i)) {
		res = max(res, tree[i]);
	}
	return res;
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)cin >> w[i];
	memcpy(q, w, sizeof(w));
	sort(q, q + n);
	m = unique(q, q + n) - q;
	LL ans = 0;
	for (int i = 0; i < n; i++) {
		int x = lower_bound(q, q + m, w[i]) - q + 1;
		LL sum = get_sum(x - 1) + w[i];
		ans = max(sum, ans);
		add(x, sum);
	}
	cout << ans << endl;
	return 0;
}